package com.bjsxt.aspect;

import com.baomidou.mybatisplus.extension.api.IErrorCode;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.bjsxt.model.R;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

/**
 * 1 内部API异常 处理
 */
@ExceptionHandler(value  = ApiException.class)
    public R handlerApiException(ApiException exception){
    IErrorCode errorCode = exception.getErrorCode();
    if(errorCode!=null){
        return  R.fail(errorCode);
    }
    return R.fail(exception.getMessage());

}

/**
 * 2 方法参数校验失败异常
 */
@ExceptionHandler(MethodArgumentNotValidException.class)
public R handlerMethodArgumentNotValidException(MethodArgumentNotValidException exception)
{
    BindingResult bindingResult = exception.getBindingResult();
    if(bindingResult.hasErrors())
    {
        FieldError fieldError = bindingResult.getFieldError();
        if(fieldError != null){
            return R.fail(fieldError.getField()+fieldError.getDefaultMessage());
        }
    }
    return R.fail(exception.getMessage());
}

/**
 * 对象内部使用Validate 没有校验成功异常
 */
    @ExceptionHandler(BindException.class)
    public R handlerBindException(BindException exception)
    {
        BindingResult bindingResult = exception.getBindingResult();
        if(bindingResult.hasErrors())
        {
            FieldError fieldError = bindingResult.getFieldError();
            if(fieldError != null){
                return R.fail(fieldError.getField()+fieldError.getDefaultMessage());
            }
        }
        return R.fail(exception.getMessage());



    }



}
